package io.xxx.omni.oss.sdk

import com.google.gson.JsonObject
import io.xxx.omni.oss.common.toJsonObject
import io.xxx.omni.oss.domain.Store
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.web.client.RestTemplate
import org.springframework.web.util.UriComponentsBuilder

@Component
class WmClient {

    @Autowired
    private lateinit var restTemplate: RestTemplate

    protected val httpUrl = "https://dopen.weimob.com/api/1_0/ec"

    fun execute(store: Store, path: String, request: Map<String, Any>): JsonObject {
        val authorization = store.authorization
        val url = UriComponentsBuilder.fromHttpUrl(httpUrl)
            .path(path)
            .queryParam("accesstoken", authorization["accesstoken"])
            .toUriString()
        return restTemplate.postForEntity(url, request, String::class.java).body!!.toJsonObject()
    }
}